package com.wangdao.wechatmall.controller.admin;

import com.wangdao.wechatmall.bean.bo.admin.KeyWordBO;
import com.wangdao.wechatmall.bean.bo.admin.KeyWordParamBO;
import com.wangdao.wechatmall.bean.pojo.Keyword;
import com.wangdao.wechatmall.bean.vo.admin.BaseRespVo;
import com.wangdao.wechatmall.bean.vo.admin.KeyWordRespVO;
import com.wangdao.wechatmall.bean.vo.admin.KeyWordVO;
import com.wangdao.wechatmall.exception.AdminVarcharException;
import com.wangdao.wechatmall.service.admin.KeyWordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * KeyWord Controller
 * created by feng on 7/9/21
 */

@RestController
@RequestMapping("admin/keyword")
public class KeyWordController {

    @Autowired
    KeyWordService keyWordService;

    //http://localhost:8083/admin/keyword/list?page=1&limit=20&sort=add_time&order=desc
    //http://localhost:8083/admin/keyword/list?page=1&limit=20&keyword=1&sort=add_time&order=desc
    //get
    @GetMapping("list")
    public BaseRespVo list(KeyWordBO keyWordBO) {
        KeyWordVO keyWordVO = keyWordService.getKeyWord(keyWordBO);
        return BaseRespVo.ok(keyWordVO);
    }

    //http://localhost:8083/admin/keyword/delete
    @PostMapping("delete")
    public BaseRespVo delete(@RequestBody Keyword keyword) {
        System.out.println(keyword);
        int effectRow = keyWordService.delete(keyword);

        if (effectRow == 1) {
            return BaseRespVo.ok();
        }

        return BaseRespVo.fail("删除失败");
    }

    //http://localhost:8083/admin/keyword/create
    @PostMapping("create")
    public BaseRespVo create(@RequestBody KeyWordParamBO keywordParamBO) throws AdminVarcharException {

        // keyword 过长
        if (keywordParamBO.getKeyword() != null && !"".equals(keywordParamBO.getKeyword()) && keywordParamBO.getKeyword().length() > 127) {
            throw new AdminVarcharException("关键词过长, 请调整后再输入");
        }

        // getUrl 过长
        if (keywordParamBO.getUrl() != null && !"".equals(keywordParamBO.getUrl()) && keywordParamBO.getUrl().length() > 255) {
            throw new AdminVarcharException("跳转链接过长, 请调整后再输入");
        }

        KeyWordRespVO keyWordRespVO = keyWordService.create(keywordParamBO);

        return BaseRespVo.ok(keyWordRespVO);
    }

    //http://localhost:8083/admin/keyword/update
    @PostMapping("update")
    public BaseRespVo update(@RequestBody Keyword keyword) throws AdminVarcharException {

        // keyword 过长
        if (keyword.getKeyword() != null && !"".equals(keyword.getKeyword()) && keyword.getKeyword().length() > 127) {
            throw new AdminVarcharException("关键词过长, 请调整后再输入");
        }

        // getUrl 过长
        if (keyword.getUrl() != null && !"".equals(keyword.getUrl()) && keyword.getUrl().length() > 255) {
            throw new AdminVarcharException("跳转链接过长，请调整后再输入");
        }

        Keyword returnKeyword = keyWordService.update(keyword);

        return BaseRespVo.ok(returnKeyword);
    }


}